package com.java.basis.thread;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author wangning
 * @create 2021-04-25 9:12
 */
public class TestThread01 {
	public static void main(String[] args) {
		//通过Runnable
		MyRunnable myRunnable = new MyRunnable();
		Thread t1=new Thread(myRunnable);
		t1.start();
		//以下的方法可与上边的线程并发执行
		doSomething();

		//通过Callable
		MyCallable myCallable = new MyCallable();
		FutureTask<String> futureTask=new FutureTask<String>(myCallable);
		Thread thread=new Thread(futureTask);
		thread.start();//开启一个线程方法
		//以下的方法可与上边的线程并发执行
		doSomething();
		try {
			String result = futureTask.get();//获取线程返回值
			System.out.println(Thread.currentThread());
			System.out.println("result = " + result);
		} catch (InterruptedException | ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static void doSomething() {
		System.out.println("doSomething()");
	}
}
